<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Helpers\ApiReturn;
use App\Http\Requests\Api\v1\User\RegisterRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends BaseController
{
    /**
     * 用户注册
     */
    public function register(RegisterRequest $request)
    {
        $user = User::create($request->all());

        return $this->success($user);

    }

    /**
     *
     * 用户登录
     */
    public function login(Request $request)
    {
        $token = Auth::guard('api')->attempt(['email' => $request->email, 'password' => $request->password]);

        if ($token) {

            return $this->success('bearer ' . $token);

        }

        return $this->failed('账号或者密码错误');
    }

    /**
     * 用户退出
     */
    public function logout()
    {
        Auth::guard('api')->logout();

        return $this->success([], '退出成功');
    }

    /**
     * 用户信息
     */
    public function show()
    {
        $user = Auth::guard('api')->user();

        return $this->success($user);
    }
}
